﻿using Aspose.Words;
using Castle.DynamicProxy;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.IBLL;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CNKI.TPI.Web.UI.Controllers.Search
{
    public class TopicViewController : BaseController
    {
        INaviService naviService = null;
        IUserService userService = null;
        ISearchService searchService = null;
        IFileService fileService = null;
        public TopicViewController(INaviService naviService, IUserService userService, ISearchService searchService, IFileService fileService)
        {
            this.naviService = generator.CreateInterfaceProxyWithTarget<INaviService>(naviService, new IInterceptor[] { new LogInterceptor() });
            this.userService = generator.CreateInterfaceProxyWithTarget<IUserService>(userService, new IInterceptor[] { new LogInterceptor() });
            this.searchService = generator.CreateInterfaceProxyWithTarget<ISearchService>(searchService, new IInterceptor[] { new LogInterceptor() });
            this.fileService = generator.CreateInterfaceProxyWithTarget<IFileService>(fileService, new IInterceptor[] { new LogInterceptor() });
        }
        //
        // GET: /TopicView/

        public ActionResult Index(string dbName, int resourceId, int recordId, int dbId, int downType)
        {
            ViewBag.dbId = dbId;
            //ViewBag.DownloadCAJ = "javascript:alert('附件不存在')";
            //ViewBag.DownloadPDF = "javascript:alert('附件不存在')";
            //int fileId = 0;
            //IList<METADATA_FILE> list = searchService.GetFile(recordId, dbName);
            //if (list != null)
            //{
            //    foreach (METADATA_FILE file in list)
            //    {
            //        if (file.FORMAT == "CAJ" || file.FORMAT == "PDF")
            //        {
            //            ViewBag.Download = ResourceHelper.GetFileDownloadURL(dbId, file.SYSID);
            //            fileId = file.SYSID;
            //            break;
            //        }
            //    }
            //}
            int fileId = resourceId;
            ViewBag.Download = ResourceHelper.GetFileDownloadURL(dbId, fileId, recordId, downType);
            ViewBag.DownType = downType;
            ViewBag.dbName = dbName;
            ViewBag.RecordID = recordId;

            if (fileId != 0)
            {
                ViewBag.AttachId = fileId;
            }
            else
            {
                ViewBag.AttachId = 0;
            }

            string filePath = fileService.GetDigitalFile(dbId, ViewBag.AttachId);
            if (resourceId == -1)
            {
                filePath = fileService.GetFirstDigitalFile(dbId, recordId);
            }

            //将word转换成pdf
            string filename = Path.GetFileNameWithoutExtension(filePath);
            string pdfPath = System.Web.HttpContext.Current.Server.MapPath("~/pdf/" + filename + ".pdf");
            if (!ResourceHelper.CheckFileExist(pdfPath))
            {
                if (filePath.IndexOf(".pdf") > 0)
                {
                    System.IO.File.Copy(filePath, pdfPath);
                }
                else
                {
                    CreatePDF(filePath, pdfPath);
                }
            }

            if ((!string.IsNullOrEmpty(pdfPath)) && ResourceHelper.CheckFileExist(pdfPath))
            {
                ViewBag.IsFileExsit = true;
            }
            else
            {
                ViewBag.IsFileExsit = false;
            }
            ViewBag.OnlineViewUrl = "../pdf/" + filename + ".pdf";
            return View();
        }

        public bool CreatePDF(string soursePath, string savePath)
        {
            try
            {
                Document doc = new Document(soursePath);
                //保存PDF文件  
                doc.Save(savePath);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

    }
}
